home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / comm / mail / YamNet.lha / FSCode.lha / Src / Encode.c < prev    next >
C/C++ Source or Header  |  1995-11-02  |  3KB  |  169 lines

  1. /*
  2. ** $VER: Encode.c (22.5.95) by Flavio Stanchina
  3. */
  4.  
  5. #include <exec/types.h>
  6. #include <dos/dos.h>
  7. #include <dos/stdio.h>
  8.  
  9. #include <clib/exec_protos.h>
  10. #include <clib/utility_protos.h>
  11. #include <clib/dos_protos.h>
  12.  
  13. #include <string.h>
  14.  
  15. #if defined(__SASC)
  16. #include <proto/exec.h>
  17. #include <proto/utility.h>
  18. #include <proto/dos.h>
  19. #endif
  20.  
  21. #include "FSCode.h"
  22. #include "CRC32.h"
  23.  
  24. static BPTR OpenOut(struct FSData *fsd)
  25. {
  26.     fsd->Part += 1;
  27.  
  28.     if (fsd->To)
  29.     {
  30.         if (fsd->Multi)
  31.             SPrintf(fsd->Temp, NameFmt, fsd->To, fsd->Part);
  32.         else
  33.             strcpy(fsd->Temp, fsd->To);
  34.  
  35.         if (fsd->Out = Open(fsd->Temp, MODE_NEWFILE))
  36.         {
  37.             SetVBuf(fsd->Out, NULL, BUF_FULL, 16384);
  38.             fsd->close_out = TRUE;
  39.         }
  40.         else
  41.         {
  42.             MyPrintFault(fsd, IoErr(), fsd->Temp);
  43.             return 0;
  44.         }
  45.     }
  46.     else fsd->Out = Output();
  47.  
  48.     FPutC(fsd->Out, '\n');
  49.  
  50.     if (fsd->Multi)
  51.         FPrintf(fsd->Out, MultiFmt, fsd->Part, fsd->Parts, FilePart(fsd->File));
  52.     else
  53.         FPrintf(fsd->Out, StartFmt, FilePart(fsd->File));
  54.  
  55.     return fsd->Out;
  56. }
  57.  
  58. static VOID FlushOut(struct FSData *fsd)
  59. {
  60.     if (fsd->TempCnt)
  61.     {
  62.         FWrite(fsd->Out, fsd->Temp, 1, fsd->TempCnt);
  63.         FPutC (fsd->Out, '\n');
  64.  
  65.         fsd->Line   += 1;
  66.         fsd->TempCnt = 0;
  67.     }
  68. }
  69.  
  70. static VOID CloseOut(struct FSData *fsd)
  71. {
  72.     if (fsd->Out)
  73.     {
  74.         FlushOut(fsd);
  75.         FPrintf(fsd->Out, EndFmt, fsd->Size, fsd->CRC);
  76.         if (fsd->close_out) MyClose(fsd, fsd->Out);
  77.         fsd->Out = 0;
  78.     }
  79. }
  80.  
  81. VOID DoEncode(STRPTR out, ULONG in, struct Library *UtilBase);
  82. //{
  83. //    out[4] = (in % 85) + 42; in /= 85;
  84. //    out[3] = (in % 85) + 42; in /= 85;
  85. //    out[2] = (in % 85) + 42; in /= 85;
  86. //    out[1] = (in % 85) + 42; in /= 85;
  87. //    out[0] = (in     ) + 42;
  88. //}
  89.  
  90. LONG Encode(struct FSData *fsd)
  91. {
  92.     ULONG *in;
  93.     UBYTE *out;
  94.     ULONG tmp = 0;
  95.  
  96.     fsd->Size = 0;
  97.     fsd->CRC  = 0xFFFFFFFF; /* preload shift register, per CRC-32 spec */
  98.  
  99.     out = fsd->Temp;
  100.  
  101.     while((LONG)(fsd->BufCnt = Read(fsd->In, fsd->Buffer, FSD_BUFSIZ)) > 0)
  102.     {
  103.         in = (ULONG *)fsd->Buffer;
  104.  
  105.         do {
  106.             if ((fsd->TempCnt == 0) && (fsd->Line == 0))
  107.                 OpenOut(fsd);
  108.  
  109.             if (fsd->BufCnt < 4)
  110.             {
  111.                 fsd->CRC = CRC32_blocks((UBYTE *)in, fsd->BufCnt, fsd->CRC);
  112.  
  113.                 tmp = *in;
  114.                 switch(fsd->BufCnt)
  115.                 {
  116.                     case 1: tmp >>= 8;
  117.                     case 2: tmp >>= 8;
  118.                     case 3: tmp >>= 8;
  119.                 }
  120.  
  121.                 DoEncode(out, tmp, UtilityBase);
  122.  
  123.                 switch(fsd->BufCnt)
  124.                 {
  125.                     case 1: out[2] = '#';
  126.                     case 2: out[1] = '#';
  127.                     case 3: out[0] = '#';
  128.                 }
  129.  
  130.                 fsd->Size  += fsd->BufCnt;
  131.                 fsd->BufCnt = 0;
  132.             }
  133.             else
  134.             {
  135.                 fsd->CRC = CRC32_blocks((UBYTE *)in, 4, fsd->CRC);
  136.                 DoEncode(out, *in++, UtilityBase);
  137.  
  138.                 fsd->Size   += 4;
  139.                 fsd->BufCnt -= 4;
  140.             }
  141.  
  142.             out += 5;
  143.             fsd->TempCnt += 5;
  144.  
  145.             if (fsd->TempCnt == 75)
  146.             {
  147.                 if (CheckSignal(SIGBREAKF_CTRL_C)) return -3;
  148.  
  149.                 FlushOut(fsd);
  150.                 out = fsd->Temp;
  151.  
  152.                 // close block only if more than one line left
  153.                 if (fsd->Multi && (fsd->Line >= fsd->Lines) && ((fsd->RealSize - fsd->Size) > 60))
  154.                 {
  155.                     CloseOut(fsd);
  156.                     fsd->Line = 0;
  157.                 }
  158.             }
  159.         } while(fsd->BufCnt > 0);
  160.     }
  161.  
  162.     if ((LONG)(fsd->BufCnt) == -1)
  163.         MyPrintFault(fsd, IoErr(), "read error");
  164.  
  165.     CloseOut(fsd);
  166.  
  167.     return 0;
  168. }
  169.